<?php
session_start();
/*if (!isset($_SESSION['level']) || $_SESSION['level'] == ''){
	echo "<script type='text/javascript'>window.location='../index.php';</script>"; 
}
$level = $_SESSION['level'];*/
  // Các giá trị dược lưu trong biến $_POST
  // Kiểm tra nếu được post
  if(isset($_REQUEST['id'])) {
	$id = $_REQUEST['id']; //id ma~ san pham 
  		include_once('controller/db.php');
		$db = new DB;
		$result = $db->execute("select * from sanpham where MaSanPham = $id ");
		$row=mysql_fetch_array($result);
      // Đưa dữ liệu vào các biến
	    $MaSanPham = $row['MaSanPham'];
		$TenSP = $row['TenSP'];
		$GiaCa = $row['GiaCa'];
		$LoaiHinh = $row['LoaiHinh'];
		$NoiBan = $row['NoiBan'];	
		$HangSX = $row['HangSX'];	
		$BaoHanh = $row['BaoHanh'];
		$MaLoaiSP = $row['MaLoaiSP'];
		$TrangThai = $row['TrangThai'];
		$MoTaChiTiet = $row['MoTaChiTiet'];
		$TuKhoaTimKiem = $row['TuKhoaTimKiem'];
 }

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8" />
    <title>Create 1</title>
    <link rel="stylesheet" type="text/css" href="../css/design.css">
	<script type="text/javascript" src="../css/js/jquery.js"></script>
	<script type="text/javascript" src="../css/js/jquery.validate.min.js"></script>
	<script type="text/javascript" src="../css/localization/messages_vi.js"></script>
      <style type="text/css">
        body{margin: 0; padding: 0; font: 13px Arial, Helvetica, sans-serif; background: url(../css/bg.jpg) top center no-repeat fixed; color: #333333;}
      </style>
  </head>
  <body>
    <div class="login_container">
      <div class="login_header">
        <div class="logo"></div>
      </div>
      <div class="admin">
	      <form id="sanpham_edit" class="fedit" action="sanpham_edit.php" method="post">
			<fieldset><legend>SỬA THÔNG TIN SẢN PHẨM</legend>
				<p style="display:none;"><label for="name">Mã sản phẩm</label>
				<input type="text" name="id" id="id" value="<?php echo $id ?>"/></p>
			    <p><label for="name">Tên Sản Phẩm</label>
			    <input type="text" name="TenSP" id="TenSP" class="required" value="<?php echo $TenSP ?>" /></p>
				<p><label for="name">Giá cả</label>
			    <input type="text" name="GiaCa" id="GiaCa" class="required number" value="<?php echo $GiaCa ?>"/></p>
				<p><label for="name">Loại hình</label>
			    <input type="text" name="LoaiHinh" id="LoaiHinh" class="required" value="<?php echo $LoaiHinh ?>" /></p>
				<p><label for="name">Nơi bán</label>
			    <input type="text" name="NoiBan" id="NoiBan" class="required" value="<?php echo $NoiBan ?>" /></p>
				<p><label for="name" class="l2">Hãng sản xuất</label>
			    <input type="text" name="HangSX" id="HangSX" class="required" value="<?php echo $HangSX ?>" /></p>
                <p><label for="name" class="l2">Bảo hành</label>
			    <input type="text" name="BaoHanh" id="BaoHanh" class="required" value="<?php echo $BaoHanh ?>" /></p>
                <p><label for="name" class="l2">Mã loại sản phẩm</label>
			    <input type="text" name="MaLoaiSP" id="MaLoaiSP" class="required number" value="<?php echo $MaLoaiSP ?>" /></p>
                <p><label for="name" class="l2">Trạng thái</label>
			    <input type="text" name="TrangThai" id="TrangThai" class="required number" value="<?php echo $TrangThai ?>" /></p>
                <p><label for="name" class="l2">Mô tả chi tiết</label>
			    <input type="text" name="MoTaChiTiet" id="MoTaChiTiet" class="required" value="<?php echo $MoTaChiTiet ?>" /></p>
                <p><label for="name" class="l2">Từ khoá tìm kiếm</label>
			    <input type="text" name="TuKhoaTimKiem" id="TuKhoaTimKiem" class="required" value="<?php echo $BaoHanh ?>" /></p>
			    <p class="submit"><input id="submit" type="submit" value="Đồng ý" /></p>
			</fieldset>
      </div>
    </div>
  </body>
</html>
<?php
    if(isset($_POST["id"]))
    {
		$MaSanPham = $_POST["id"];
        $TenSP = $_POST["TenSP"];
		$GiaCa = $_POST["GiaCa"];
		$LoaiHinh = $_POST["LoaiHinh"];
		$NoiBan = $_POST["NoiBan"];	
		$HangSX = $_POST["HangSX"];	
		$BaoHanh = $_POST["BaoHanh"];
		$MaLoaiSP = $_POST["MaLoaiSP"];
		$TrangThai = $_POST["TrangThai"];
		$MoTaChiTiet = $_POST["MoTaChiTiet"];
		$TuKhoaTimKiem = $_POST["TuKhoaTimKiem"];
        $result2 = $db->execute("update sanpham set TenSP = '$TenSP', GiaCa = '$GiaCa', LoaiHinh = '$LoaiHinh', NoiBan = '$NoiBan', HangSX = '$HangSX', BaoHanh = '$BaoHanh', MaLoaiSP = '$MaLoaiSP', TrangThai = '$TrangThai', MoTaChiTiet = '$MoTaChiTiet', TuKhoaTimKiem = '$TuKhoaTimKiem' where MaSanPham = $MaSanPham");
		if(!$result2)
			echo "<script type='text/javascript'>alert('>>> ( Failed !!! ) <<<'); window.location='sanpham.php';</script>"; 
		echo "<script type='text/javascript'>window.location='sanpham.php';</script>";
		$db->disConnect();
    }
?>
<script>
$(document).ready(function(){
	// Validate form
	$("#sanpham_edit").validate({
		errorElement: "span", // Định dạng cho thẻ HTML hiện thông báo lỗi
	});
});
</script>